/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import businessObjects.Admin;
import businessObjects.Esquema;
import daos.AdminDAO;
import daos.EsquemaDAO;
import daos.exceptions.NonexistentEntityException;
import daos.exceptions.RollbackFailureException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author williamprado
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class AdminServices implements AdminServicesLocal {
    
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @PostConstruct
    public void initialize(){
    entityManager = entityManagerFactory.createEntityManager();
    }

    @PreDestroy
    public void cleanUp(){
        if(entityManager.isOpen()){
            entityManager.close();
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    public void nuevoAdmin(Admin admin) {
        AdminDAO ad = new AdminDAO();
        //boolean success = false;
        try {
            ad.create(admin, entityManager);
        //} catch (PreexistingEntityException ex) {
          //  Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public Admin login(String login, String pass){
        AdminDAO ad = new AdminDAO();
        Admin admin = new Admin();
        try{
            admin = ad.findAdmin(login, entityManager);
        }catch (Exception e) {
            return null;
        }
        if(admin != null){
            if (admin.getPassword().equals(pass)){
                return admin;
            }else{return null;}
        }else{return null;}
    }

    public Admin consultarAdmin(String id) {
        AdminDAO ad = new AdminDAO();
        Admin admin = new Admin();
        try {
            admin = ad.findAdmin(id, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return admin;
    }

    public void actualizarAdmin(Admin admin) {
        AdminDAO ad = new AdminDAO();
        try {
            ad.edit(admin, entityManager);
        //} catch (NonexistentEntityException ex) {
          //  Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void eliminarAdmin(String id) {
        AdminDAO ad = new AdminDAO();
        try {
            //try {
            ad.destroy(id, entityManager);
            //} catch (NonexistentEntityException ex) {
            //  Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
            //}
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int leerNumeroFilasBasic(){
        int num = 1;
        EsquemaDAO ed = new EsquemaDAO();
        int esq = 1;
        try {
            num = ed.findEsquema(esq, entityManager).getFilas();
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
        }
        return num;
    }

    public void cambiarNumeroFilasBasic(int filas){
        try {
            EsquemaDAO ed = new EsquemaDAO();
            Esquema esqBasic = ed.findEsquema(1, entityManager);
            System.out.println("servicio: kosas antes: "+esqBasic.getIdEsquema()+" "+esqBasic.getNombreEsquema()+" "+esqBasic.getFilas());
            esqBasic.setFilas(filas);
            System.out.println("servicio: recibi filas: "+esqBasic.getFilas()+" costo: "+esqBasic.getCostoDiario());
            ed.edit(esqBasic, entityManager);
        } catch (Exception ex) {
            Logger.getLogger(AdminServices.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("error!!");
        }
    }
 
}
